package LeetcodeTest_thirty_eight;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

//面试题 17.10. 主要元素
public class majorityElement
{
    public static void main(String[] args)
    {
        int[] nums={1,2,1,1,3,3,1};
        System.out.println(Solution.majorityElement(nums));
    }
}
class Solution
{
    public static int majorityElement(int[] nums)
    {
        HashMap<Integer,Integer> map=new HashMap<>();
        int n=nums.length;
        for(int i=0;i<n;i++)
        {
            if(!map.containsKey(nums[i]))
            {
                map.put(nums[i],1);
            }
            else
            {
                int count=map.get(nums[i])+1;
                map.put(nums[i],count);
            }
        }
        Set<Integer> integers = map.keySet();
        for(Integer key:integers)
            if(map.get(key)>n/2)
                return key;
        return -1;
    }
}